package com.crm.business.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.crm.business.admin.service.AdminService;
import com.crm.business.crm.mapper.CrmHeadquartersBusinessBalanceMapper;
import com.crm.business.crm.service.CrmPageService;
import com.crm.business.crm.service.ICrmHeadquartersBusinessBalanceService;
import com.crm.common.crm.constant.CrmEnum;
import com.crm.common.crm.constant.FieldEnum;
import com.crm.common.crm.entity.BO.CrmModelSaveBO;
import com.crm.common.crm.entity.BO.CrmRoiSearchBO;
import com.crm.common.crm.entity.BO.CrmUpdateInformationBO;
import com.crm.common.crm.entity.PO.CrmHeadquartersBusinessBalance;
import com.crm.common.crm.entity.VO.CrmFieldSortVO;
import com.crm.common.crm.entity.VO.CrmModelFiledVO;
import com.crm.common.ka.core.entity.BasePage;
import com.crm.business.servlet.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class CrmHeadquartersBusinessBalanceServiceImpl extends BaseServiceImpl<CrmHeadquartersBusinessBalanceMapper, CrmHeadquartersBusinessBalance> implements ICrmHeadquartersBusinessBalanceService, CrmPageService {
    @Autowired
    private AdminService adminService;

    @Override
    public String[] appendSearch() {
        return new String[0];
    }

    @Override
    public void setOtherField(Map<String, Object> map) {

    }

    @Override
    public CrmEnum getLabel() {
        return null;
    }

    public CrmHeadquartersBusinessBalance queryById(Integer id) {
        return null;
    }

    public void deleteByIds(List<Integer> ids) {
        removeByIds(ids);
    }

    /**
     * 列表查询
     * @param search
     * @return
     */
    @Override
    public BasePage<CrmHeadquartersBusinessBalance> queryPageList(CrmRoiSearchBO search) {
        Integer pageStart = search.getPage() > 0 ? (search.getPage() - 1) * search.getLimit() : 0;
        search.setPageStart(pageStart);

        List<CrmHeadquartersBusinessBalance> crmAllianceBusinessList = getBaseMapper().queryPageList(search);

        BasePage<CrmHeadquartersBusinessBalance> basePage = new BasePage<>(search.getPage(),search.getLimit(),crmAllianceBusinessList.size());
        Integer total = getBaseMapper().queryRow();
        basePage.setTotal(total);
        basePage.setList(crmAllianceBusinessList);
        return basePage;
    }


    public void addOrUpdate(CrmModelSaveBO crmModel) {
        CrmHeadquartersBusinessBalance crmAllianceBusiness = BeanUtil.copyProperties(crmModel.getEntity(),CrmHeadquartersBusinessBalance.class);
        if(crmAllianceBusiness.getId() != null){
            // 编辑
            updateById(crmAllianceBusiness);
        }else{
            // 新增
            save(crmAllianceBusiness);
        }
    }

    public void updateInformation(CrmUpdateInformationBO updateInformationBO) {

    }

    /**
     * 查询所有字段
     *
     * @return data
     */
    public List<CrmModelFiledVO> queryField(Integer id) {
        return queryDefaultField();
    }

    /**
     * 下载导入模板
     *
     * @param response 线索id
     */
    public void downloadExcel(HttpServletResponse response) throws IOException {

    }

    public List<CrmFieldSortVO> queryListHead(){
        List<CrmFieldSortVO> list = new ArrayList<>();
        list.add(new CrmFieldSortVO().setName("投放平台").setType(FieldEnum.SELECT.getType()).setFieldName("putinPlatform").setFormType(FieldEnum.SELECT.getFormType()));
        list.add(new CrmFieldSortVO().setName("大类").setType(FieldEnum.TEXT.getType()).setFieldName("category").setFormType(FieldEnum.TEXT.getFormType()));
        list.add(new CrmFieldSortVO().setName("渠道充值金额").setType(FieldEnum.FLOATNUMBER.getType()).setFieldName("putinMoney").setFormType(FieldEnum.FLOATNUMBER.getFormType()));
        list.add(new CrmFieldSortVO().setName("渠道消耗金额").setType(FieldEnum.FLOATNUMBER.getType()).setFieldName("practicalPutinMoney").setFormType(FieldEnum.FLOATNUMBER.getFormType()));
        list.add(new CrmFieldSortVO().setName("渠道返点金额").setType(FieldEnum.FLOATNUMBER.getType()).setFieldName("putinRebatesMoney").setFormType(FieldEnum.FLOATNUMBER.getFormType()));
        list.add(new CrmFieldSortVO().setName("渠道赔付金额").setType(FieldEnum.FLOATNUMBER.getType()).setFieldName("putinCompensateMoney").setFormType(FieldEnum.FLOATNUMBER.getFormType()));
        list.add(new CrmFieldSortVO().setName("渠道转入金额").setType(FieldEnum.FLOATNUMBER.getType()).setFieldName("rollinMoney").setFormType(FieldEnum.FLOATNUMBER.getFormType()));
        list.add(new CrmFieldSortVO().setName("渠道转出金额").setType(FieldEnum.FLOATNUMBER.getType()).setFieldName("rolloutMoney").setFormType(FieldEnum.FLOATNUMBER.getFormType()));
        list.add(new CrmFieldSortVO().setName("备注").setType(FieldEnum.TEXT.getType()).setFieldName("remark").setFormType(FieldEnum.TEXT.getFormType()));
        list.add(new CrmFieldSortVO().setName("表单录入时间").setType(FieldEnum.DATE.getType()).setFieldName("putinFormDate").setFormType(FieldEnum.DATE.getFormType()));
        list.add(new CrmFieldSortVO().setName("创建时间").setType(FieldEnum.DATETIME.getType()).setFieldName("createTime").setFormType(FieldEnum.DATETIME.getFormType()));
        list.add(new CrmFieldSortVO().setName("创建人").setType(FieldEnum.TEXT.getType()).setFieldName("putinUserName").setFormType(FieldEnum.TEXT.getFormType()));
        list.add(new CrmFieldSortVO().setName("审核状态").setType(FieldEnum.TEXT.getType()).setFieldName("checkStatus").setFormType(FieldEnum.TEXT.getFormType()));

        return list;
    }

    public void exportExcel(HttpServletResponse response, CrmRoiSearchBO search){

    }

    /**
     * 查询所有字段
     *
     * @return data
     */
    @Override
    public List<CrmModelFiledVO> queryDefaultField() {
        //查询 线索 自定义字段 中 客户来源 作为 投放平台下拉选项
        List<CrmModelFiledVO> filedList = new ArrayList<>();
        //投放金额
        filedList.add(new CrmModelFiledVO("putinMoney", FieldEnum.FLOATNUMBER,"投放金额",1).setIsNull(1).setFormType(FieldEnum.FLOATNUMBER.getFormType()));
        //实际投放金额
        filedList.add(new CrmModelFiledVO("practicalPutinMoney", FieldEnum.FLOATNUMBER,"实际投放金额",1).setIsNull(1).setFormType(FieldEnum.FLOATNUMBER.getFormType()));
        //课程期数
        filedList.add(new CrmModelFiledVO("coursePeriods", FieldEnum.TEXT,"课程期数",1).setIsNull(1).setIsUnique(1).setFormType(FieldEnum.TEXT.getFormType()));
        //投放日期
        filedList.add(new CrmModelFiledVO("putinDate", FieldEnum.TEXT,"投放日期",1).setIsNull(1).setIsUnique(1).setFormType(FieldEnum.TEXT.getFormType()));
        return filedList;
    }

    public CrmHeadquartersBusinessBalance getListById(Integer id){
        return null;
    }

    /**
     * 根据加盟商Id查询余额信息
     * @param deptId
     * @return
     */
    public CrmHeadquartersBusinessBalance queryDataByDeptId(Integer deptId, String source){
        return this.baseMapper.queryDataByDeptId(deptId,source);
    }

    /**
     * 根据组织Id查询合计余额信息
     * @param deptIds
     * @return
     */
    public BigDecimal queryBalanceByDeptIds(List<Integer> deptIds){
        return this.baseMapper.queryBalanceByDeptIds(deptIds);
    }

    /**
     * 根据组织Id更新加盟商余额
     * @param deptId
     * @param balance
     */
    public void updateBalanceByDeptId(Integer deptId, BigDecimal balance, String topUpSource){
        this.baseMapper.updateBalanceByDeptId(deptId,balance,topUpSource);
    }
}
